//
//  ViewController.m
//  乐账
//
//  Created by 谢毅 on 16/11/7.
//  Copyright © 2016年 xieyi. All rights reserved.
//

#import "ViewController.h"
#import "UI_ViewController.h"
#import "DUI_ViewController.h"
#import "webViewController.h"
#import "OCRViewController.h"
#import "PicViewController.h"
#import "MapViewController.h"
#import "GeocoderController.h"
#import "BaiduMapViewController.h"

#import "UIButton+UIButton_Extension.h"
#import "UILabel+UILabel_Extension.h"
#import "UIColor+UIColor_Extension.h"
#import "UICollectionViewFlowLayout+Extension.h"

#import "QRView.h"
#import "AdscrollView.h"
#import "BannerView.h"
#import "ToolMenuCell.h"
#import "AppcollectionCell.h"
#import "ToolCellModel.h"

/// 工具栏Cell重用标志符号
static NSString * ToolMenuCellId = @"ToolMenuCellId";
/// 小应用Cell重用标志符
static NSString * applicationCellId = @"applicationCellId";

@interface ViewController ()<QRViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)NSArray *ToolimageNameArry;
@property(nonatomic,strong)NSArray *AppImageNameArry;
@property(nonatomic,strong)NSArray *AppImageNameArry_CN;
@property(nonatomic,strong)NSArray *scrollViewArray;
@property(nonatomic,strong)ToolCellModel *cellmodel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareDate];//加载数据
}

/// 准备界面需要的的数据
-(void)prepareDate{
    ///广告轮播器图片
    self.scrollViewArray = @[@"1_pic.jpg",@"2_pic.jpg",@"3_pic.jpg",@"4_pic.jpg",@"6_pic.jpg"];
    /// 工具栏图片数组
    self.ToolimageNameArry = @[@"aquarius",@"aries",@"cancer",@"capricorn",@"gemini",@"leo",@"libra",@"pisces",@"sagittarius",@"scorpio",@"taurus",@"virgo"];
    /// 小应用图标数组
    self.AppImageNameArry = @[@"calculator2",@"camera",@"email",@"maps",@"mms",@"settings",@"weather",@"calculator2",@"camera",@"email",@"maps",@"mms",@"settings",@"weather",@"calculator2",@"camera",@"email",@"maps",@"mms",@"settings",@"weather",@"compose_pic_add_highlighted"];
    self.AppImageNameArry_CN = @[@"计算器",@"相机",@"邮件",@"地图",@"信息",@"设置",@"天气",@"计算器",@"相机",@"邮件",@"地图",@"信息",@"设置",@"天气",@"计算器",@"相机",@"邮件",@"地图",@"信息",@"设置",@"天气",@" "];

}
-(ToolCellModel*)cellmodel{
    if (_cellmodel == nil) {
        _cellmodel = [[ToolCellModel alloc] init];
    }
    return _cellmodel;
}
//加载界面控件的元素
-(void)loadView{
    [super loadView];
    [self prepareDate];//加载数据
    [self prepareForUI];
    [self setupUI];
}
-(void)prepareForUI{
    [self.navigationController.navigationBar setTranslucent:NO];
    NSString *tmp  = [[UIDevice currentDevice] systemVersion];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //设置颜色
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRGB:118 green:187 blue:224 alpha:1];
    //设置状态栏颜色
    UIView * statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.view.bounds.size.width, 20)];
    statusBar.backgroundColor = [UIColor colorWithRGB:118 green:187 blue:224 alpha:1];
    [self.navigationController.navigationBar addSubview:statusBar];
    //_UINavigationBarBackground将该属性设置为透明，这是苹果没有开放的属性
    [self.navigationController.navigationBar subviews][0].alpha = 0;
    //设置控制器背景颜色
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
}
/// 添加界面控件
-(void)setupUI{
    
    CGFloat freedom = 10;//控件之间间隙
    CGFloat margin = [UIScreen mainScreen].scale *4;
    /// 1.导航栏控件设置
    // 在导航栏左边上添加二维码扫描图标
    UIButton *Scan = [UIButton imageButton:CGRectMake(0, 0, 20, 20) setBackgroundImage:@"Scan" addTarget:self action:@selector(QR)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Scan];
    //在导航栏右边添加搜索按钮
    UIButton *Search = [UIButton imageButton:CGRectMake(0, 0, 30, 30) setBackgroundImage:@"tabbar_discover" addTarget:self action:@selector(pic)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Search];
    
    ///2.添加轮播器
    BannerView *scrollView = [[BannerView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 150)];
    scrollView.imageArray = self.scrollViewArray;

    ///3.设置工具菜单标题视图
    UIView *ToolTitleView = [[UIView alloc] initWithFrame:CGRectZero];
    ToolTitleView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    ToolTitleView.layer.cornerRadius = margin;
    UILabel *ToolTitle = [[UILabel alloc] initWithFrame:CGRectZero];
    
    ToolTitle.text = @"亲,你好我是工具栏";
    ToolTitle.textAlignment = NSTextAlignmentCenter;
    [ToolTitleView addSubview:ToolTitle];
    [ToolTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ToolTitleView.mas_top);
        make.left.equalTo(ToolTitleView.mas_left);
        make.right.equalTo(ToolTitleView.mas_right);
        make.bottom.equalTo(ToolTitleView.mas_bottom);
    }];

    
    //添加中部工具菜单
    CGFloat conut = 7;
    CGFloat w = (self.view.bounds.size.width - (conut + 1) * margin) / conut;
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout createFlowLayout: CGSizeMake(w, w) sectionInset:UIEdgeInsetsMake(margin, margin, margin, margin) minRowMargin:margin minColMargin:margin scrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 60) collectionViewLayout:flowLayout];
    
    //工具视图属性设置
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerNib:[UINib nibWithNibName:@"ToolMenuCell" bundle:nil] forCellWithReuseIdentifier:ToolMenuCellId];
    
    collectionView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    collectionView.layer.cornerRadius = margin;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.bounces = NO;
    collectionView.tag = 1;
   
    //设置小应用菜单标题视图
    UIView *AppTitleView = [[UIView alloc] initWithFrame:CGRectZero];
    AppTitleView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    AppTitleView.layer.cornerRadius = margin;
    UILabel *AppTitle = [[UILabel alloc] initWithFrame:CGRectZero];
    AppTitle.text = @"亲,你好我是小应用列表";
    AppTitle.textAlignment = NSTextAlignmentCenter;
    [AppTitleView addSubview:AppTitle];
    [AppTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(AppTitleView.mas_top);
        make.left.equalTo(AppTitleView.mas_left);
        make.right.equalTo(AppTitleView.mas_right);
        make.bottom.equalTo(AppTitleView.mas_bottom);
    }];

    
    //添加中部小应用applicationView
    UICollectionViewFlowLayout *appflowLayout = [[UICollectionViewFlowLayout alloc] init];
    UICollectionView * applicationView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 60) collectionViewLayout:appflowLayout];
    //appflowLayout属性设置
    NSLog(@"%f---%f",w,margin);
      CGFloat colum = 4;
      CGFloat width = 50;
      CGFloat height = 65;
    CGFloat margin_H = (applicationView.bounds.size.width - 4*width)/(colum + 1);
    appflowLayout.itemSize = CGSizeMake(width, height);//应用显示3行
    appflowLayout.sectionInset = UIEdgeInsetsMake(margin, margin_H, margin, margin_H);
    appflowLayout.minimumInteritemSpacing = margin_H/2;
    appflowLayout.minimumLineSpacing = margin;
    appflowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //applicationView属性设置
    applicationView.showsVerticalScrollIndicator = NO;
   //注册cell
    [applicationView registerNib:[UINib nibWithNibName:@"AppcollectionCell" bundle:nil] forCellWithReuseIdentifier:applicationCellId];
    applicationView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
    applicationView.layer.cornerRadius = margin;
    applicationView.bounces = NO;
    applicationView.pagingEnabled = YES;
    applicationView.dataSource = self;
    applicationView.delegate = self;
    applicationView.tag = 2;
    
    //添加控件
    [self.view addSubview:scrollView];
    [self.view addSubview:ToolTitleView];
    [self.view addSubview:collectionView];
    [self.view addSubview:AppTitleView];
    [self.view addSubview:applicationView];
    
    
    ///  自动布局
    ///广告轮播器视图（后期改进）
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@150);
        
    }];
    //工具菜单标题视图
    [ToolTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scrollView.mas_bottom).offset(freedom);
        make.left.equalTo(self.view.mas_left).offset(freedom);
        make.right.equalTo(self.view.mas_right).offset(-freedom);
        make.height.equalTo(@30);
    }];
    //工具菜单
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ToolTitleView.mas_bottom).offset(freedom/2);
        make.left.equalTo(self.view.mas_left).offset(freedom);
        make.right.equalTo(self.view.mas_right).offset(-freedom);
        make.height.equalTo(@64);
        
    }];
//
    //小应用标题视图
    [AppTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(collectionView.mas_bottom).offset(1.5*freedom);
        make.left.equalTo(self.view.mas_left).offset(freedom);
        make.right.equalTo(self.view.mas_right).offset(-freedom);
        make.height.equalTo(@30);
    }];
    //小应用列表
    [applicationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(AppTitleView.mas_bottom).offset(freedom/2);
        make.left.equalTo(self.view.mas_left).offset(freedom);
        make.right.equalTo(self.view.mas_right).offset(-freedom);
        make.height.equalTo(@(3*height+4*margin));
        
    }];
    
    

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   
}
//按键响应函数
-(void)UI{
    //有UI识别
    UI_ViewController *VC = [[UI_ViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)DUI{
    //无UI识别
    DUI_ViewController *VC = [[DUI_ViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}
-(void)webviewLoad{
    //无UI识别
    webViewController *VC = [[webViewController alloc] init];
    [self presentViewController:VC animated:YES completion:nil];
}

-(void)QR{
    //二维码
    self.navigationItem.leftBarButtonItem.enabled = NO;
    QRView *AVCapView = [[QRView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
    AVCapView.delegate = self;//设置代理
    [self.view addSubview:AVCapView];
    AVCapView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        AVCapView.alpha =1.0;
    }];
    
}

-(void)callBack:(QRView *)qrView{
    [qrView removeFromSuperview];
    self.navigationItem.leftBarButtonItem.enabled = YES;
}


-(void)OCR{
    //文字识别
    OCRViewController *VC = [[OCRViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)pic{
    //相册
    PicViewController *VC = [[PicViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)Map{
    //地图
    MapViewController *VC = [[MapViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)Geocoder{
    //地图地址解析
    GeocoderController *VC = [[GeocoderController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)BaiduMap{
    //baidu地图
    BaiduMapViewController *VC = [[BaiduMapViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark -代理方法
//组数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
//每组个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return collectionView.tag ==1 ? self.ToolimageNameArry.count:self.AppImageNameArry.count;
}
//每行要显示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%ld",(long)collectionView.tag);
    if (collectionView.tag == 1) {
        
    ToolMenuCell*ToolCell = [collectionView dequeueReusableCellWithReuseIdentifier: ToolMenuCellId forIndexPath:indexPath];
    ToolCell.namestring = self.ToolimageNameArry[indexPath.item];
        return ToolCell;
        
    }else{
    AppcollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: applicationCellId forIndexPath:indexPath];
        
        cell.iconImageString = self.AppImageNameArry[indexPath.item];
        cell.iconNameString = self.AppImageNameArry_CN[indexPath.item];
       
       return cell;
    }
    
}
/// 处理点击items事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //1.判断是哪个collectionView的点击事件
    //2.判断是哪个item 的点击
    if (collectionView.tag == 1) {
        NSLog(@"十二星座的：%@",self.ToolimageNameArry[indexPath.item]);
    }else if (collectionView.tag == 2) {
        switch (indexPath.item) {
            case 0:
                [self webviewLoad];
                break;
            case 1:
                [self pic];
                break;
            case 2:
                [self DUI];
                break;
            case 3:
                [self BaiduMap];
                break;
            case 4:
                [self Geocoder];
                break;
            case 5:
                [self UI];
                break;
            default:
                break;
        }
    
    }
    

}









@end
